package junghoAndI;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;

import java.awt.Dimension;

import javax.swing.JLabel;
import javax.swing.border.LineBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableModel;

import java.awt.Color;

import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JComboBox;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.swing.DefaultComboBoxModel;

import junghoAndI.DogVO;
import junghoAndI.DogDAO;
import junghoAndI.DogMain;

import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;



public class InsertDog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField imgIn;
	private JTextField dnameTextField;
	private JTextField age;
	private JTextField collar;
	private JTextField specificloc;
	private JTextField etc;
	private DogDAO ddao = new DogDAO();
	private DogMain main= new DogMain();
	private JTextField sno;
	private JTextField tno;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			InsertDog dialog = new InsertDog(null, null);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 * @param model 
	 * @param model 
	 */
	public InsertDog(final DefaultTableModel model, final JTable dogTable) {
		setTitle(":: 유기견 등록 ::");
		setBounds(100, 100, 600, 800);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.WEST);
		
		final JLabel image = new JLabel("");
		image.setBorder(new LineBorder(Color.GRAY, 3));
		image.setPreferredSize(new Dimension(200, 300));
		
		imgIn = new JTextField();
		imgIn.setColumns(10);
		
		final JButton imgInBtn = new JButton("사진등록");
		imgInBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (imgInBtn.getText().isEmpty()) {
					JOptionPane.showMessageDialog(null, "사진 이름을 넣어주세요");
					return;
				}
				FileNameExtensionFilter filter = 
						new FileNameExtensionFilter("이미지 파일", "png", "jpg", "gif");
				JFileChooser fc = new JFileChooser();
				fc.setFileFilter(filter);
				int choice = fc.showOpenDialog(InsertDog.this);
				if ( choice == JFileChooser.APPROVE_OPTION ) {
					String img = fc.getSelectedFile().getPath();
					String myPhoto = fc.getSelectedFile().getName();
					String savePath = "c:/Java/myPhoto" + myPhoto;
					imgIn.setText(savePath);					
					BufferedImage images = null;
					try {
						File origin = new File(img);
						images = ImageIO.read(origin);
						ImageIO.write(images, "png", new File(savePath));
					} catch (Exception ex) {
					}
					if (DogGalleryDAO.insertPhoto(imgIn.getText(), myPhoto) > 0) {
						JOptionPane.showMessageDialog(null, "이미지 등록!");
					}
					image.setIcon(new ImageIcon(savePath));
				} // if
				
			}
		});
		
		JLabel txtDname = new JLabel("이름");
		
		JLabel txtSex = new JLabel("성별");
		
		JLabel txtType = new JLabel("종류");
		
		JLabel txtAge = new JLabel("나이");
		
		JLabel txtSno = new JLabel("담당보호소");
		
		JLabel txtSpecificloc = new JLabel("구체적위치");
		
		JLabel txtFno = new JLabel("발견지");
		
		JLabel txtCollar = new JLabel("개목걸이번호");
		
		JLabel txtEtc = new JLabel("비고");
		
		JLabel txtMercykilling = new JLabel("안락사여부");
		
		dnameTextField = new JTextField();
		dnameTextField.setColumns(10);
		
		final JRadioButton isHe = new JRadioButton("남");
		
		final JRadioButton isShe = new JRadioButton("여");
		
		age = new JTextField();
		age.setColumns(10);
		
		collar = new JTextField();
		collar.setColumns(10);
		
		specificloc = new JTextField();
		specificloc.setColumns(10);
		
		
		final JComboBox snoCbx = new JComboBox();
		snoCbx.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				sno.setText((String)snoCbx.getSelectedItem());
			}
		});
		snoCbx.setModel(new DefaultComboBoxModel(new String[] {"담당보호소", "한국동물구조관리협회", "카라(사)동물보호시민단체", "동물학대방지연합", "부산동물학대방지연합", "대전동물보호센터", "동물보호센터", "한국동물보호협회", "동물자유연대", "부산유기동물보호협회", "하늘아이", "동물사랑실천협회", "캐비클럽", "동물보호관리시스템", "(사)한국야생동물보호협회"}));
		
		etc = new JTextField();
		etc.setColumns(10);
		
		final JComboBox mercykillingCbx = new JComboBox();
		mercykillingCbx.setModel(new DefaultComboBoxModel(new String[] {"안락사check", "아니오", "예"}));
		
		JLabel imagepathLabel = new JLabel("저장할 위치");
		
		final JComboBox fnoCbx = new JComboBox(new Object[]{});
		fnoCbx.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3", "4"}));
		
		final JComboBox tnoCbx = new JComboBox(new Object[]{});
		tnoCbx.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				tno.setText((String)tnoCbx.getSelectedItem());
			}
		});
		tnoCbx.setModel(new DefaultComboBoxModel(new String[] {"강아지 종류", "시베리안허스키", "치와와", "시츄", "불독", "그냥 개", "푸들", "닥스훈트", "진돗개", "삽살개", "도베르만"}));
		
		sno = new JTextField();
		sno.setColumns(10);
		
		tno = new JTextField();
		tno.setColumns(10);
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(39)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING, false)
									.addComponent(txtSex)
									.addComponent(txtDname)
									.addComponent(txtType)
									.addComponent(txtAge)
									.addComponent(txtCollar, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
									.addComponent(txtFno))
								.addComponent(txtSpecificloc)
								.addComponent(txtSno)
								.addComponent(txtEtc, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)
								.addGroup(gl_contentPanel.createSequentialGroup()
									.addComponent(txtMercykilling, GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)
									.addPreferredGap(ComponentPlacement.RELATED)))
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPanel.createSequentialGroup()
									.addComponent(isHe)
									.addGap(19)
									.addComponent(isShe))
								.addComponent(age, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(dnameTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addGroup(gl_contentPanel.createParallelGroup(Alignment.TRAILING, false)
									.addComponent(etc, Alignment.LEADING)
									.addComponent(specificloc, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE))
								.addComponent(mercykillingCbx, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(fnoCbx, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)
								.addComponent(collar, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(sno, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addGroup(gl_contentPanel.createSequentialGroup()
									.addGap(1)
									.addComponent(tno, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
									.addGap(18)
									.addComponent(tnoCbx, GroupLayout.PREFERRED_SIZE, 124, GroupLayout.PREFERRED_SIZE)))
							.addGap(213))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(image, GroupLayout.PREFERRED_SIZE, 122, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
								.addComponent(imgInBtn)
								.addComponent(imagepathLabel)
								.addGroup(gl_contentPanel.createParallelGroup(Alignment.TRAILING)
									.addComponent(snoCbx, GroupLayout.PREFERRED_SIZE, 201, GroupLayout.PREFERRED_SIZE)
									.addComponent(imgIn, GroupLayout.PREFERRED_SIZE, 314, GroupLayout.PREFERRED_SIZE)))
							.addGap(96))))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(18)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtDname)
						.addComponent(dnameTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtSex)
						.addComponent(isHe)
						.addComponent(isShe))
					.addGap(28)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtType)
						.addComponent(tnoCbx, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(tno, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(27)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtAge)
						.addComponent(age, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(28)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtCollar)
						.addComponent(collar, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(36)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtFno)
						.addComponent(fnoCbx, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(29)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtSpecificloc)
						.addComponent(specificloc, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtSno)
						.addComponent(sno, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(snoCbx, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(26)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtEtc)
						.addComponent(etc, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(30)
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
								.addComponent(txtMercykilling)
								.addComponent(mercykillingCbx, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addGap(33)
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
								.addComponent(image, GroupLayout.PREFERRED_SIZE, 176, GroupLayout.PREFERRED_SIZE)
								.addGroup(gl_contentPanel.createSequentialGroup()
									.addComponent(imagepathLabel)
									.addGap(13)
									.addComponent(imgIn, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(200)
							.addComponent(imgInBtn)))
					.addGap(108))
		);
		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton insertBtn = new JButton("등록");
				insertBtn.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						JOptionPane.showMessageDialog(null, "등록하시겠습니까?");
						
						String gender = null;
						
						if(isHe.isSelected())
							gender = "남";
						else if(isShe.isSelected())
							gender = "여";
						
					DogVO m = new DogVO(null,dnameTextField.getText(),gender, ddao.findTnoDog((String)tnoCbx.getSelectedItem()), age.getText(), null, collar.getText(), (String)fnoCbx.getSelectedItem(), specificloc.getText(), ddao.findSnoDog((String)snoCbx.getSelectedItem()), etc.getText(), (String)mercykillingCbx.getSelectedItem(), imgIn.getText());  
					// 선택한 텍스트 값을 받아서 VO에 저장할 때 다시 숫자 값으로 받아서 지정
					
					if(ddao.insertDog(m)>0){
						JOptionPane.showMessageDialog(null, "유기견 등록완료!");
						dogTable.repaint();
						dogTable.setModel(model);
						
						dispose();
					}
					main = new DogMain();
					main.setVisible(true);
					
				
					}
					
					
				});
				insertBtn.setActionCommand("OK");
				buttonPane.add(insertBtn);
				getRootPane().setDefaultButton(insertBtn);
			}
			{
				JButton cancelBtn = new JButton("취소");
				cancelBtn.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
					
						dispose();
						main = new DogMain();
						main.setVisible(true);
					}
				});
				cancelBtn.setActionCommand("Cancel");
				buttonPane.add(cancelBtn);
			}
		}
	}
}
